Process : Multi-Process management

更新时间:
2024-05-14
下载文档

Process : Multi-Process management

process is a multi-process management module, This module only allows Privileged Mode application use.

User can use the following code to import the process module.

var process = require('process');

Support

The following shows process module APIs available for each permissions.

 User ModePrivilege Mode
process.me
process.parent
process.exit
process.platform
process.monoTime
process.detach 
process.spawn 
process.isChild 
process.kill 
process.termClear 
process.termSigno 
process.priority 
process.guarder 
process.setenv
process.getenv
process.unsetenv
process.environ

Process Object

process.me()

  • Returns: {Integer} Process ID.

Get the current process ID number.

Example

console.log('Current PID:', process.me());

process.parent()

  • Returns: {Integer} Parent process ID.

Get the parent process ID number. If zero is returned, the current process is an orphan process.

Example

console.log('My parent PID:', process.parent());

process.exit([code])

  • code {Integer} Process exit code 0 ~ 255. default: 0.

Current process exit. All tasks in the current process will be killed immediately.

process.platform()

  • Returns: {String} Current operating system kernel name.

Get operating system kernel name. 'SylixOS' will be returned now.

process.monoTime()

  • Returns: {Integer} Current process running time in milliseconds.

Get current process running time (consume cpu time).

process.detach([pid])

  • pid {Integer} Process ID. default: current process.
  • Returns: {Boolean} Whether the operation was successful.

Make the specified process an orphan process, suppress parent-child relationship.

process.spawn(path[, argArray[, envArray[, workDir[, stdFiles]]]])

  • path {String} Executable file path that new require process execution.
  • argArray {Array} Parameter string array. default: no parameter.
  • envArray {Array} Array of environment variables. default: inherit the current environment variable.
  • workDir {String} New process working path. default: inherit the current working path.
  • stdFiles {Array} Array of standard file path. default: inherit the current process standard files.
  • Returns: {Integer} The new process ID, less than or equal to 0, indicates that the spawn failed.

Create a new process to run the specified executable file.

Example

var pid = process.spawn('./hello.js', ['./hello.js', '-a', 'abcd']);
if (pid > 0) {
  console.log(pid);
}

process.spawn(opt[, argArray[, envArray[, workDir[, stdFiles]]]])

  • opt {Object} Spawn options.
    • path {String} Executable file path that new require process execution.
    • stack {Integer} SylixOS process main thread stack size.
  • argArray {Array} Parameter string array. default: no parameter.
  • envArray {Array} Array of environment variables. default: inherit the current environment variable.
  • workDir {String} New process working path. default: inherit the current working path.
  • stdFiles {Array} Array of standard file path. default: inherit the current process standard files.
  • Returns: {Integer} The new process ID, less than or equal to 0, indicates that the spawn failed.

Create a new process to run the specified executable file with specified options. EdgerOS 1.8.7 and later support.

process.isChild(pid)

  • pid {Integer} Process ID.
  • Returns: {Boolean} Whether the process is a child process.

Determine if the specified process is a child process.

Example

var pid = process.spawn('./hello.js', ['./hello.js']);
if (pid > 0) {
  process.isChild(pid);
  // true
}

process.kill(pid[, sigNo])

  • pid {Integer} Process ID.
  • sigNo {Integer} POSIX signal number. default: process.SIGINT.
  • Returns: {Boolean} Whether the signal is sent successfully.

Example

var pid = process.spawn('./hello.js', ['./hello.js']);
if (pid > 0) {
  sys.sleep(5000); // Run 5 seconds.
  process.kill(pid);
}

process.termClear(pid[, remove | signo])

  • pid {Integer} Process ID.
  • remove {Boolean} Whether to remove previously added pids from the recycle list.
  • Returns: {Boolean} Whether the clear save is successfully.

Install a SIGTERM signal callback. When the current process receives the SIGTERM signal, it will kill the specified child process and then exit. Kill child process to also use SIGTERM signal.

Example

var pid = process.spawn('./hello.js', ['./hello.js']);
if (pid > 0) {
  process.termClear(pid); // use SIGTERM
  process.termClear(pid, process.SIGINT); // use SIGINT
}

process.termSigno(pid, signo)

Reserved function.

process.priority(pid, prio)

  • pid {Integer} Process ID.
  • prio {String} New priority.

prio Can only be:

  • 'low'
  • 'normal'
  • 'high'
  • 'realtime'

Set the specified process priority, the priority of all tasks in the process will be set.

process.guarder()

  • Returns: {Boolean} Whether it is set successfully.

Set the current process as the memory guard process. The memory guard process can monitor various memory thresholds of the system and perform related recycling control.

Process Environment Variable

Unprivileged apps cannot obtain or change the environment variables originally inherited by the current process, and can only set or modify environment variables created by themselves.

process.setenv(env, value[, overwrite])

  • env {String} Environment variable name.
  • value {String} New variable value.
  • overwrite {Boolean} Whether to overwrite if exist. default: true.
  • Returns: {Boolean} Whether it is set successfully.

Set the value of an environment variable, only valid for the current process.

Example

process.setenv('TEST', 'value');

process.getenv(env)

  • env {String} Environment variable name.
  • Returns: {String} The value of the environment variable, undefined indicates that the specified environment variable does not exist.

Get the value of the environment variable.

Example

process.setenv('TEST', 'value');
console.log(process.getenv('TEST'));

process.unsetenv(env)

  • env {String} Environment variable name.
  • Returns: {Boolean} Whether it is remove successfully.

Delete a specified environment variable.

process.environ([array])

  • array {Boolean} Whether to output as an array mode. default: false.
  • Returns: {Object} | {Array} All environment variables.

Get all environment variables of the current process.

var obj = process.environ();
console.log(obj);
// {AAA:V1, BBB:V2, CCC:V3, ...}

var array = process.environ(true);
// ['AAA=v1', 'BBB=V2', 'CCC=V3', ...]

Process Events

The process object inherits from the EventEmitter class. The following events are thrown in some specific situations.

will

When EdgerOS App's will description is true, before it is killed, it has a few seconds to perform a key state save operation and can leave its own last words. When this event occurs, the current process will be killed after a few seconds (typically 3 seconds).

Example

var important = 'important data!';

process.on('will', function() {
  fs.writeFile('./save', important);
  process.exit();
});

child

When the child process exits, a child event will be generated. By obtaining this event parameter, it will be replaced with the corresponding process exit information.

Example

process.on('child', function(child) {
  console.log('Child process pid:', child.pid, 'exited with return value:', child.status);
});

detach

This event is received when the child process detaches with the current process and the previously set child process recycling function will be invalid.

Example

process.on('detach', function(child) {
  console.log('Child process pid:', child.pid, 'detached!');
});

lowmem

The EdgerOS operating system allows you to set page fault guards for different modes of program collections. Privileged programs can receive warning messages through process.guarder() settings. This event will be received when the physical memory lower then threshold previously set.

Example

process.on('lowmem', function(free) {
  console.log(`The system does not have enough memory, and currently has ${free} Bytes left!`);
});
文档内容是否对您有所帮助?
有帮助
没帮助